package com.basic.technology.study.introduction.thread.lock;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLock {

    private ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();

    public static void main(String[] args) {

        ReadWriteLock readWriteLock = new ReadWriteLock();
        new Thread(){
            @Override
            public void run() {
                readWriteLock.get(Thread.currentThread());
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                readWriteLock.get(Thread.currentThread());
            }
        }.start();
    }

    public void get(Thread thread) {
        rwl.readLock().lock();
        try {
            long l = System.currentTimeMillis();
            while (System.currentTimeMillis() - l <= 100) {
                System.out.println(thread.getName() + "正在进行操作");
            }
            System.out.println(thread.getName()+"操作完毕");
        } finally {
            rwl.readLock().unlock();
        }
    }

}
